<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="wx_openid" prop="wxOpenid">
      <el-input v-model="dataForm.wxOpenid" placeholder="wx_openid"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="学号" prop="studentId">
      <el-input v-model="dataForm.studentId" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="regdate">
      <el-input v-model="dataForm.regdate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="likesNum">
      <el-input v-model="dataForm.likesNum" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="提问数" prop="questionsNum">
      <el-input v-model="dataForm.questionsNum" placeholder="提问数"></el-input>
    </el-form-item>
    <el-form-item label="回复数" prop="answersNum">
      <el-input v-model="dataForm.answersNum" placeholder="回复数"></el-input>
    </el-form-item>
    <el-form-item label="年级" prop="grade">
      <el-input v-model="dataForm.grade" placeholder="年级"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="college">
      <el-input v-model="dataForm.college" placeholder="学院"></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="major">
      <el-input v-model="dataForm.major" placeholder="专业"></el-input>
    </el-form-item>
    <el-form-item label="学历" prop="education">
      <el-input v-model="dataForm.education" placeholder="学历"></el-input>
    </el-form-item>
    <el-form-item label="个性签名" prop="signature">
      <el-input v-model="dataForm.signature" placeholder="个性签名"></el-input>
    </el-form-item>
    <el-form-item label="被举报次数" prop="reports">
      <el-input v-model="dataForm.reports" placeholder="被举报次数"></el-input>
    </el-form-item>
    <el-form-item label="收藏数" prop="collections">
      <el-input v-model="dataForm.collections" placeholder="收藏数"></el-input>
    </el-form-item>
    <el-form-item label="总阅读数" prop="readings">
      <el-input v-model="dataForm.readings" placeholder="总阅读数"></el-input>
    </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          wxOpenid: '',
          phoneNumber: '',
          studentId: '',
          nickname: '',
          avatar: '',
          gender: '',
          regdate: '',
          likesNum: '',
          questionsNum: '',
          answersNum: '',
          grade: '',
          college: '',
          major: '',
          education: '',
          signature: '',
          reports: '',
          collections: '',
          readings: '',
          sort: ''
        },
        dataRule: {
          wxOpenid: [
            { required: true, message: 'wx_openid不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          studentId: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          regdate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          likesNum: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          questionsNum: [
            { required: true, message: '提问数不能为空', trigger: 'blur' }
          ],
          answersNum: [
            { required: true, message: '回复数不能为空', trigger: 'blur' }
          ],
          grade: [
            { required: true, message: '年级不能为空', trigger: 'blur' }
          ],
          college: [
            { required: true, message: '学院不能为空', trigger: 'blur' }
          ],
          major: [
            { required: true, message: '专业不能为空', trigger: 'blur' }
          ],
          education: [
            { required: true, message: '学历不能为空', trigger: 'blur' }
          ],
          signature: [
            { required: true, message: '个性签名不能为空', trigger: 'blur' }
          ],
          reports: [
            { required: true, message: '被举报次数不能为空', trigger: 'blur' }
          ],
          collections: [
            { required: true, message: '收藏数不能为空', trigger: 'blur' }
          ],
          readings: [
            { required: true, message: '总阅读数不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/main/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.wxOpenid = data.user.wxOpenid
                this.dataForm.phoneNumber = data.user.phoneNumber
                this.dataForm.studentId = data.user.studentId
                this.dataForm.nickname = data.user.nickname
                this.dataForm.avatar = data.user.avatar
                this.dataForm.gender = data.user.gender
                this.dataForm.regdate = data.user.regdate
                this.dataForm.likesNum = data.user.likesNum
                this.dataForm.questionsNum = data.user.questionsNum
                this.dataForm.answersNum = data.user.answersNum
                this.dataForm.grade = data.user.grade
                this.dataForm.college = data.user.college
                this.dataForm.major = data.user.major
                this.dataForm.education = data.user.education
                this.dataForm.signature = data.user.signature
                this.dataForm.reports = data.user.reports
                this.dataForm.collections = data.user.collections
                this.dataForm.readings = data.user.readings
                this.dataForm.sort = data.user.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/main/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'wxOpenid': this.dataForm.wxOpenid,
                'phoneNumber': this.dataForm.phoneNumber,
                'studentId': this.dataForm.studentId,
                'nickname': this.dataForm.nickname,
                'avatar': this.dataForm.avatar,
                'gender': this.dataForm.gender,
                'regdate': this.dataForm.regdate,
                'likesNum': this.dataForm.likesNum,
                'questionsNum': this.dataForm.questionsNum,
                'answersNum': this.dataForm.answersNum,
                'grade': this.dataForm.grade,
                'college': this.dataForm.college,
                'major': this.dataForm.major,
                'education': this.dataForm.education,
                'signature': this.dataForm.signature,
                'reports': this.dataForm.reports,
                'collections': this.dataForm.collections,
                'readings': this.dataForm.readings,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
